const path = require('path')
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[chunkhash:5].js'
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    },
    extensions: ['.js', '.css'], // 解析扩展名
  },
  module: {
    rules: [
      {
        test: /\.(css|less)$/i,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              modules: true
            }
          }, 'less-loader'
        ]
      },
      {
        test: /\.(png|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'images/[name][hash:5].[ext]'
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(), // 清除dist目录
    new HtmlWebpackPlugin({ // 生成html文件
      template: './public/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: 'styles/[name].[contenthash:5].css'
    })
  ],
  devServer: {
    port: 1022,
    open: true,
    hot: true,
    proxy: [
      {
        '/api': {
          target: 'http://localhost:3000',
          changeOrigin: true,
        }
      }
    ],
  },
}
